/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.arthas.psimobile.fragment.login;

import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;

import com.arthas.psimobile.core.BaseFragment;
import com.xuexiang.psimobile.R;
import com.xuexiang.psimobile.databinding.FragmentRegisterBinding;
import com.arthas.psimobile.utils.XToastUtils;
import com.xuexiang.xaop.annotation.SingleClick;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.utils.CountDownButtonHelper;
import com.xuexiang.xui.utils.ResUtils;
import com.xuexiang.xui.utils.ThemeUtils;
import com.xuexiang.xui.widget.actionbar.TitleBar;


/**
 * 注册页面
 *
 * @author Arthas
 * @since 2021-4-1 14:46
 */
@Page(anim = CoreAnim.none)
public class RegisterFragment extends BaseFragment<FragmentRegisterBinding> implements View.OnClickListener {

    private CountDownButtonHelper mCountDownHelper;

    @Override
    protected TitleBar initTitle() {
        TitleBar titleBar = super.initTitle()
                .setImmersive(true);
        titleBar.setBackgroundColor(Color.TRANSPARENT);
        titleBar.setTitle("");
        titleBar.setLeftImageDrawable(ResUtils.getVectorDrawable(getContext(), R.drawable.ic_login_close));
        titleBar.setActionTextColor(ThemeUtils.resolveColor(getContext(), R.attr.colorAccent));
        return titleBar;
    }

    @SingleClick
    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.btn_register) {
            if (binding.etPhoneNumber.validate()) {
                if (binding.etPasswd.validate()) {
                    if (binding.etConfirmPasswd.validate()) {
                        if (binding.etConfirmPasswd.getEditValue().equals(binding.etPasswd.getEditValue())) {
                            if (binding.etVerifyCode.validate()) {
                                CertificationFragment.RegisterInfo tmp = new CertificationFragment.RegisterInfo();
                                tmp.phoneNumber = binding.etPhoneNumber.getEditValue();
                                tmp.passwd = binding.etPasswd.getEditValue();
                                openNewPage(CertificationFragment.class, "registerInfo", tmp);
                            }
                        } else {
                            XToastUtils.warning("两次密码输入不一致");
                        }
                    }
                }
            }
        } else if (id == R.id.btn_get_verify_code) {
            if (binding.etPhoneNumber.validate()) {
                getVerifyCode(binding.etPhoneNumber.getEditValue());
            }
        } else if (id == R.id.tv_login) {
            CertificationFragment.RegisterInfo tmp = new CertificationFragment.RegisterInfo();
            openPage(LoginFragment.class);
        }
    }

    @NonNull
    @Override
    protected FragmentRegisterBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentRegisterBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        mCountDownHelper = new CountDownButtonHelper(binding.btnGetVerifyCode, 60);
    }

    @Override
    protected void initListeners() {
        binding.btnRegister.setOnClickListener(this);
        binding.tvLogin.setOnClickListener(this);
        binding.btnGetVerifyCode.setOnClickListener(this);
    }

    /**
     * 获取验证码
     */
    private void getVerifyCode(String phoneNumber) {
        XToastUtils.warning("只是演示，验证码随便输");
        mCountDownHelper.start();
    }

}
